package com.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.core.util.Assert;

import com.easywan.utils.Config;



public class ImageGet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static Logger logger = Logger.getLogger(ImageGet.class.getName());
    
    // 定义允许上传的文件扩展名
    private String Ext_Name = "gif,jpg,jpeg,png,bmp,swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2";

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    	try {
			showImage(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

   
    public void showImage(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
	    response.setContentType("text/html; charset=UTF-8");
	    response.setContentType("image/jpeg");
	    String fname = request.getParameter("iname");
	    if(fname == null || "".equals(fname)){
	    	return;
	    }
	    String newpath = new String(fname.getBytes("ISO-8859-1"), "UTF-8");
	    String absolutePath = "";
	    if(!"dest".equals(request.getParameter("dest"))){
	    	absolutePath = Config.get("imgPath") + newpath;
	    }else{
	    	absolutePath = Config.get("imgDestPath") + newpath;
	    }
	    
	    FileInputStream fis = new FileInputStream(absolutePath);
	    OutputStream os = response.getOutputStream();
	    try
	    {
	    int count = 0;
	    byte[] buffer = new byte[1024 * 1024];
	    while ((count = fis.read(buffer)) != -1)
	    os.write(buffer, 0, count);
	    os.flush();
	    }
	    catch (IOException e)
	    {
	    e.printStackTrace();
	    }
	    finally
	    {
	    if (os != null)
	    os.close();
	    if (fis != null)
	    fis.close();
	    }
    }

}